import { Message } from 'element-ui';
export default {
    props: ['data'],
    name: 'budgetBaseInfo',
    data() {
        return {
            builtImg: {},
            formModel: {
                constructionProjectName: '',
                constructionProjectAddress: '',
                constructionProjectArea: '',
                designFiles: '',
                budgetType: ''
            },
            uploadHttp: '/api/attachment/upload',
            formList: [
                { label: '工程名称', prop: 'constructionProjectName' },
                { label: '工程地址', prop: 'constructionProjectAddress' },
                { label: '工程面积', prop: 'constructionProjectArea' },
                { label: '施工图', prop: 'designFiles', type: 'text' },
                {
                    label: '预算清单类型',
                    prop: 'budgetType',
                    type: 'select',
                    style: 'width:500px',
                    options: [
                        { label: '预算清单', value: '1' },
                        { label: '变更清单', value: '2' }
                    ]
                }
            ],
            uploadFileList: []
        };
    },
    watch: {
        'data.designFiles'(designFiles) {
            this.showFiles(designFiles);
        }
    },
    methods: {
        handleChange(response, file, fileList) {
            this.builtImg = {
                fileType: '11',
                workOrderNo: this.data.workOrderNo,
                reconnaissanceFiles: fileList.map(v => v.response).join(',') || ''
            };
        },

        //上传成功
        uploadSuccess(response) {
            if (response.code === '200') {
                let attachmentName = response.data.fileName;
                let fileUrl = process.env.VUE_APP_BASE_URL + '/api/attachment/download?fileId=' + response.data.fileId;
                let fileArr = { name: attachmentName, url: fileUrl, fileId: response.data.fileId };
                this.uploadFileList.push(fileArr);
                if (this.data.designFiles != null && this.data.designFiles !== '') {
                    this.data.designFiles += ',' + response.data.fileId;
                } else {
                    this.data.designFiles = response.data.fileId.toString();
                }
            } else {
                Message({
                    message: response.msg,
                    type: 'error'
                });
                this.removeFailUpload(this.uploadFileList);
            }
        },
        removeFailUpload(fileList) {
            for (var i = 0; i < fileList.length; i++) {
                if (fileList[i].status !== 'success') {
                    fileList.splice(i, 1);
                }
            }
        },
        getToken() {
            let result = sessionStorage.getItem('token');
            return result;
        },
        //显示附件
        showFiles(fileIds) {
            if (fileIds) {
                //获取附件名称
                this.$httpExt()
                    .get('/api/attachment/getAttachment', {
                        fileIds: fileIds,
                        _: new Date().getTime()
                    })
                    .then(
                        response => {
                            let result = response.data;
                            this.uploadFileList = [];

                            if (undefined !== result && result != null) {
                                for (let i = 0; i < result.length; i++) {
                                    let attachmentName = result[i].attachmentName;
                                    let attachmentId = result[i].attachmentId;
                                    let fileArray = fileIds.split(',');
                                    for (let j = 0; j < fileArray.length; j++) {
                                        if (Number(fileArray[j]) === attachmentId) {
                                            console.log(process.env.VUE_APP_BASE_URL);
                                            let fileUrl =
                                                process.env.VUE_APP_BASE_URL +
                                                '/api/attachment/download?fileId=' +
                                                fileArray[j];
                                            let fileArr = {
                                                name: attachmentName,
                                                url: fileUrl,
                                                fileId: fileArray[j]
                                            };
                                            this.uploadFileList.push(fileArr);
                                            break;
                                        }
                                    }
                                }
                            }
                        },
                        () => {
                            Message({
                                message: '附件服务器请求失败，请重试！',
                                type: 'warning'
                            });
                        }
                    );
            }
        },

        //附件相关操作
        beforeUpload(file) {
            return new Promise((resolve, reject) => {
                const size = file.size / 1024 / 1024;
                let fileName = file.name.toLowerCase();
                if (!/\.(jpg|jpeg|png|bmp|zip|rar|doc|xlsx|xls|docx|pdf|pptx|dwg|msg)$/.test(fileName)) {
                    Message({
                        message:
                            '只能上传jpg、jpeg、png、bmp、zip、rar、doc、xlsx、xls、docx、pdf、pptx、dwg、msg格式!',
                        type: 'error'
                    });
                    this.removeFailUpload(this.uploadFileList);
                    reject(file);
                } else if (size > 10) {
                    Message({
                        message: '上传文件大小不能超过10MB!',
                        type: 'error'
                    });
                    this.removeFailUpload(this.uploadFileList);
                    reject(file);
                } else {
                    resolve(file);
                }
            });
        },
        handlePreview(file) {
            if (file.url !== undefined && file.url !== '') {
                location.href = file.url;
            }
        }
    }
};
